package com.ondi.common.business.acmanager;

import android.app.Activity;
import android.content.Intent;

import java.util.LinkedHashSet;

public class ActivityManager {
    private static ActivityManager mActivityManager;
    private LinkedHashSet<Activity> mActivityLinkedHashSet;
    /*存储其他栈的activity*/
    private LinkedHashSet<Activity> mOtherStack;
    private Activity mBaseActivity;
    private Activity mTopMainStackActivity;
    private boolean isBackground;

    private OnLaunchListener mOnLaunchListener;
    private Activity mTargetActivity;

    private ActivityManager() {
        mActivityLinkedHashSet = new LinkedHashSet<>();
        mOtherStack = new LinkedHashSet<>();
    }

    public static ActivityManager getInstance() {
        if (mActivityManager == null) {
            synchronized (ActivityManager.class) {
                mActivityManager = new ActivityManager();
            }
        }
        return mActivityManager;
    }

    public Activity getBaseActivity() {
        return mBaseActivity;
    }
    public void setBaseActivity(Activity baseActivity) {
        mBaseActivity = baseActivity;
    }

    public Activity getTargetActivity() {
        return mTargetActivity;
    }
    public void setTargetActivity(Activity targetActivity) {
        mTargetActivity = targetActivity;
    }

    public boolean getIsBackground() {
        return isBackground;
    }

    public void addActivityByNewStack(Activity activity) {
        if (activity == null) return;
        if (mOtherStack == null) mOtherStack = new LinkedHashSet<>();

        mOtherStack.add(activity);
        addActivity(activity);
    }

    private void addActivity(Activity activity) {
        if (activity == null) return;
        if (mActivityLinkedHashSet == null) mActivityLinkedHashSet = new LinkedHashSet<>();

        mActivityLinkedHashSet.add(activity);
        if (!mOtherStack.contains(activity)) {
            mTopMainStackActivity = activity;
        }
    }

    private boolean removeActivity(LinkedHashSet<Activity> set, Activity activity) {
        if (set != null && activity != null && set.contains(activity)){
            return set.remove(activity);
        }
        return false;
    }

    public void startActivity(Activity activity, int... flagArray) {
        if (mBaseActivity == null) return;
        Intent intent = new Intent(mBaseActivity, activity.getClass());
        if (flagArray != null) {
            for (int flag : flagArray) {
                intent.addFlags(flag);
            }
        }
        mBaseActivity.startActivity(intent);
    }

    public void clearAllActivity() {
        if (mActivityLinkedHashSet != null) {
            for (Activity activity : mActivityLinkedHashSet) {
                if (activity != null && !activity.isFinishing()) {
                    activity.finish();
                }
            }
        }
    }

    public LinkedHashSet<Activity> getActivityLinkedHashSet() {
        return mActivityLinkedHashSet;
    }

    public interface OnLaunchListener {
        boolean launchFromBackground();
    }
}
